package com.example.juc.big_talk_concurrent.chapter6.article1;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Test3 {
    static CyclicBarrier cyclicBarrier = new CyclicBarrier(3);

    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("first thing finish");
                try {
                    cyclicBarrier.await(); // threadA 线程已达到屏障
                } catch (InterruptedException | BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("second thing finish");
                try {
                    cyclicBarrier.await(); // threadB 线程已达到屏障
                } catch (InterruptedException | BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        threadA.start();
        threadB.start();

        cyclicBarrier.await(); // 主线程已达到屏障
        System.out.println("FINISH");

    }
}
